<?php
/**
 * Created by PhpStorm.
 * User: 聪
 * Date: 2019/7/15
 * Time: 18:05
 */

namespace app\front\logic;


use app\front\model\Contract;

class ContractLogic extends Base
{
    public static $model = null;

    public function __construct()
    {
        self::$model = new Contract();
    }

    public static function get_list(array $request)
    {
        return static::returnJson(self::$model->allBySelect(false, ['uid' => $request['front_user']['uid']]));
    }

    public static function get_detail(array $request): string
    {
        $detail = [];
        if (isset($request['id'])) {
            self::$model->allowQuery = '*';
            $detail                  = self::$model->getOne(['id' => $request['id'], 'uid' => $request['front_user']['uid']], ['User']);
        }
        return static::returnJson([
            'detail'       => !empty($detail) ? $detail->toArray() : $detail,
            'onlineState'  => (array)self::$model->onlineState,
            'offlineState' => (array)self::$model->offlineState
        ]);
    }

    public static function add(array $request): string
    {
        $data = [
            'uid'                  => $request['front_user']['uid'],
            'signing_subject'      => $request['signing_subject'],
            'channel_company'      => $request['channel_company'],
            'channel_abbreviation' => $request['channel_abbreviation'],
            'divided_into'         => $request['divided_into'],
            'billing_cycle'        => $request['billing_cycle'],
            'validity_period'      => $request['validity_period'],
            'time_online'          => $request['time_online'],
            'offline_state'        => array_flip(self::$model->offlineState)[$request['offline_state']],
            'online_state'         => array_flip(self::$model->onlineState)[$request['online_state']],
            'game_name'            => $request['game_name'],
            'cooperation_method'   => $request['cooperation_method'],
        ];
        $ret  = self::$model->saveOne($data, isset($request['id']) ? ['id' => $request['id']] : []);
        if (!empty(self::$model->id)) {
            return static::returnSuccess(['id' => self::$model->id], 10003);
        } else {
            return static::returnJson($ret ? ['id' => $request['id']] : $ret, 10003, 20004);
        }
    }
}